/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.sdcache.distributed.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.concurrent.BlockingQueue;


/**
 * Socket Recivers<br/>
 * Recives the tags form the Media Server and puts in a Blocking Queue<br/>
 * Uses a Blocking Socket
 * @author chaitanya
 */
public class Receiver extends Thread{

	private ObjectInputStream in;
	private Socket socket;	
        private BlockingQueue inQ;
        private ClientHandler server;
        
	public Receiver(String name,Socket socket,ClientHandler server) {
            super(name+" Receiver");
                System.out.println("Afte object Stream");
                inQ=server.getInQ();
                this.server=server;
                this.socket=socket;
	}
	
        private void init()
        {
            try {
                    in = new ObjectInputStream(socket.getInputStream());

            System.out.println("1Afte object Stream");
            } catch (IOException e) {
                    e.printStackTrace();
            } 
        }
        @Override
	public void run()
	{
            init();
		try{
                    int count=0,seq=0;
			while(server.isRunning())
			{
				try {
                                        Object strTag = null;
                                        strTag = receive();
                                        if(strTag != null) 
                                        {
                                            inQ.offer(strTag);
                                            count++;
                                            if(count>100)
                                                count=0;

                                        }
                                        else
                                        {
                                            seq=0;
                                            server.setRunning(false);
                                        }
                                        Thread.sleep(1);
                                } 
                                catch (SocketTimeoutException ste) 
                                {
                                }catch(Exception e){
                                            e.printStackTrace();
                                    }
				
			}
		}
		catch(NullPointerException e){System.out.println(e);}
		catch(Exception e){System.out.println(e);}
	}

       public Object receive() throws Exception {
		Object recv = null;
		try {
			if(socket != null)
                        {
                            socket.setSoTimeout(30000);			
                            if(in != null)
                                recv = in.readObject();
                        }
                        else{
                            throw new Exception("socket null");
                        }
		} catch (SocketTimeoutException ste) {
			throw ste;
		} catch (IOException ioe) {
                        System.out.println("Io Exception for "+getName());
	    }
		return recv;
	}
	
}